import { Module } from 'vuex'
import router from '@/router'

import { IRootState } from '../types'
import { IUserState } from './types'

import {
  createProject,
  getProjectList,
  deleteProject,
  editProject,
  getProjectById,
} from '@/api/user/user'

import { ElMessage } from 'element-plus'

const userModule: Module<IUserState, IRootState> = {
  namespaced: true,
  state() {
    return {
      currentProject: () => {},
      projectList: [],
      projectCount: 0,
    }
  },

  mutations: {
    changeProjectList(state, projectList: any[]) {
      state.projectList = projectList
    },
    changeTotalCount(state, projectCount: number) {
      state.projectCount = projectCount
    },

    changeCurrentProject(state, currentProject: any) {
      state.currentProject = currentProject
    },
  },
  actions: {
    async createProjectAction({ dispatch }, payload: any) {
      // 1.创建数据请求
      const { formData } = payload
      // 2.发送请求
      const createResult = await createProject(formData)

      // 3.更新页面数据
      dispatch('getProjectListAction', {
        queryInfo: {
          offset: 1,
          size: 10,
        },
      })

      if (createResult.message === 'success') {
        ElMessage({
          message: 'create success',
          showClose: true,
          type: 'success',
        })
      } else {
        ElMessage({
          message: 'create fail',
          showClose: true,
          type: 'error',
        })
      }
      // 4.路由跳转
      router.push('/user/project-management')

      return createResult
    },

    async getProjectListAction({ commit }, payload: any) {
      // 1.解析数据
      const { queryInfo } = payload

      // 2.发送请求
      const projectListResult = await getProjectList(queryInfo)

      const { list, totalCount } = projectListResult.data

      // 3.将返回值保存到vuex中
      commit('changeProjectList', list)
      commit('changeTotalCount', totalCount)
    },

    async deleteProjectAction({ dispatch }, payload: any) {
      // 1.解析数据
      const { id } = payload

      // 2.调用删除网络请求
      const deleteResult = await deleteProject(id)
      if (deleteResult.message === 'success') {
        ElMessage({
          message: 'delete success',
          showClose: true,
          type: 'success',
        })
      } else {
        ElMessage({
          message: 'delete fail',
          showClose: true,
          type: 'error',
        })
      }
      // 3.更新页面数据
      dispatch('getProjectListAction', {
        queryInfo: {
          offset: 1,
          size: 10,
        },
      })

      return deleteResult
    },

    async editProjectAction({ dispatch }, payload: any) {
      // 1.创建数据请求
      const { id, formData } = payload
      // 2.发送请求
      const editResult = await editProject(id, formData)

      if (editResult.message === 'success') {
        ElMessage({
          message: 'update success',
          showClose: true,
          type: 'success',
        })
      } else {
        ElMessage({
          message: 'update fail',
          showClose: true,
          type: 'error',
        })
      }
      // 3.更新页面数据
      dispatch('getProjectListAction', {
        queryInfo: {
          offset: 1,
          size: 10,
        },
      })
      // 4.路由跳转
      router.push('/user/project-management')

      return editResult
    },

    async getProjectByIdAction({ commit }, payload: any) {
      // 1.解析数据
      const { id } = payload
      // 2.发送请求
      const projectResult = await getProjectById(id)
      // 3.将返回值保存到vuex中
      commit('changeCurrentProject', projectResult.data)
    },
  },
}

export default userModule
